package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.EqpUnitEntity;
import com.glory.brepda.entity.ResponseMessageEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class eqpUnitAddActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etTargetEqp)
    FreeUI_ClearEditText etTargetEqp;
    @BindView(R.id.teTargetEqpUnit)
    TextView teTargetEqpUnit;
    @BindView(R.id.teTargetEqpUnitId)
    TextView teTargetEqpUnitId;







    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_unit_add;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("加入");


    }

    @Override
    protected void initData() {
        initEdit(etTargetEqp, this::getUnitEqp);
    }



    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tvBaseSubmit) {
            if("".equals(getText(etEqpId))){
                showToast("未输入设备",false);
                return;
            }
            if("".equals(getText(etTargetEqp))){
                showToast("未输入目标设备",false);
                return;
            }
            if("".equals(teTargetEqpUnitId.getText().toString())){
                showToast("目标设备组不存在",false);
                return;
            }
            add(getText(etEqpId),teTargetEqpUnitId.getText().toString());

        }

    }

    private void add(String eqpId, String unitId) {
        FreeUI_EntityCallBack<BaseResponseBean<ResponseMessageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseMessageEntity>>
                (new TypeToken<BaseResponseBean<ResponseMessageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    ResponseMessageEntity body = response.body().getResponse().getBody();
                    showToast(body.getMESSAGE(),true);
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENT_ID",eqpId);
        map.addProperty("UNIT_ID",unitId);
        map.addProperty("ACTIONTYPE","ADD");
        NetUtils.requestPostNet("IF.EQPUNIT","Common", eqpUnitAddActivity.this,map,callBack);
    }

    private void getUnitEqp(String eqpId) {
        FreeUI_EntityCallBack<BaseResponseBean<EqpUnitEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpUnitEntity>>
                (new TypeToken<BaseResponseBean<EqpUnitEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpUnitEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpUnitEntity body = response.body().getResponse().getBody();
                    teTargetEqpUnit.setText(body.getUNITEQP());
                    teTargetEqpUnitId.setText(body.getUNITID());
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<EqpUnitEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENT_ID",eqpId);
        map.addProperty("ACTIONTYPE","QUERY");
        NetUtils.requestPostNet("IF.EQPUNIT","Common", eqpUnitAddActivity.this,map,callBack);
    }


}
